package lib.com.Business.component.register;

import lib.com.Business.dataModel.RegisterModel;
import lib.com.Dao.entity.User;
import lib.com.Dao.entity.UserInfo;
import water.com.DaoSupport.DbBase.WaterDaoSupportImp;
import water.com.DaoSupport.DbBase.WaterIDaoSupport;
import water.com.Ioc.WaterBean;

import java.util.Date;

@WaterBean
public class RegisterComponent {

    public boolean UserRegister(RegisterModel registerModel){
        WaterIDaoSupport<Object,Object> daoSupportImp = WaterDaoSupportImp.getInstance();
        User user = new User();
        user.setAccount(registerModel.getAccount());
        user.setPassword(registerModel.getPassword());
        user.setUserName(registerModel.getUserName());
        user.setTimeStamp(String.valueOf(new Date()).substring(0,26));
        try {
            daoSupportImp.insertOne(user);
            user = (User)daoSupportImp.selectOne(user);
            if(user!=null){
                UserInfo userInfo = new UserInfo();
                userInfo.setUserKey(user.getUserKey());
                userInfo.setPhone(registerModel.getPhone());
                userInfo.setTimeStamp(String.valueOf(new Date()).substring(0,26));
                daoSupportImp.insertOne(userInfo);
            }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean UserRegisterCheck(RegisterModel registerModel){
        WaterIDaoSupport<Object,Object> daoSupportImp = WaterDaoSupportImp.getInstance();
        User user = new User();
        user.setAccount(registerModel.getAccount());
        try {
            user = (User)daoSupportImp.selectOne(user);
            if(user!=null) return false;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
